home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / d3d.zip / SPHERE.C < prev    next >
Text File  |  1989-11-24  |  2KB  |  70 lines

  1. /* SPHERE: m slices, n points on each circle */
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <process.h>
  6.  
  7. main()
  8. {
  9.    FILE *fp;
  10.     int i, j, m, n, nr, next, southpole;
  11.    double r, theta, phi, pi, delphi, deltheta, rsinphi, rcosphi, x, y, z;
  12.    char str[30];
  13.  
  14.    pi = 4.0*atan(1.0);
  15.    printf("Enter m, the number of horizontal slices: "); scanf("%d", &m);
  16.    printf("\nThere will be n points on each horizontal circle.\n");
  17.    printf("It is recommended to choose n about twice as large an m.");
  18.    printf("\nEnter n: "); scanf("%d", &n);
  19.  
  20.    delphi = pi/m;
  21.    deltheta = 2*pi/n;
  22.    printf("Radius of the sphere: "); scanf("%lf",&r);
  23.    printf("Name of the output file: "); scanf("%s",str);
  24.      if((fp = fopen(str, "w")) == NULL) {
  25.       printf("File problems");
  26.       exit(1);
  27.    }
  28.    /* Vertex numbering:
  29.       i = 0:   1
  30.       i = 1:   2, 3, ..., 2n+1
  31.       i = 2:   n+2, n+3, ..., 2n+1
  32.             ...
  33.       i=m-1:   (m-2)n+2, (m-2)n+3, ..., (m-1)n+1
  34.       i=m:     (m-1)n+2
  35.    */
  36.  
  37.     fprintf(fp, "%d %f %f %f\n", 1, 0.0, 0.0, r); /* i = 0 */
  38.    for(i=1; i<m; i++) {
  39.       phi = i*delphi;
  40.       rcosphi = r*cos(phi);
  41.       rsinphi = r*sin(phi);
  42.       for(j=0; j<n; j++) {
  43.          nr = (i-1)*n + j + 2;
  44.          theta = j*deltheta;
  45.          x = rsinphi * cos(theta);
  46.          y = rsinphi * sin(theta);
  47.          z = rcosphi;
  48.             fprintf(fp, "%d %f %f %f\n", nr, x, y, z);
  49.       }
  50.    }
  51.    fprintf(fp, "%d %f %f %f\n", (m-1)*n+2, 0.0, 0.0, -r);
  52.  
  53.    fprintf(fp, "Faces:\n");
  54.     for(j=2; j<=n+1; j++)
  55.         fprintf(fp, "%d %d %d.\n", 1, j, (j<n+1 ? j+1 : 2));
  56.    for(i=1; i<m-1; i++) {
  57.       nr = (i-1)*n;
  58.       for(j=2; j<=n+1; j++) {
  59.          next = (j<n+1 ? j+1 : 2);
  60.          fprintf(fp, "%d %d %d %d.\n", nr+j, nr+n+j, nr+n+next, nr+next);
  61.       }
  62.    }
  63.    southpole = (m-1)*n + 2;
  64.    nr = (m-2)*n;
  65.    for(j=2; j<n+1; j++)
  66.       fprintf(fp, "%d %d %d.\n", nr+j, southpole, (j<n+1 ? nr+j+1 : nr + 2));
  67.    fclose(fp);
  68. }
  69.       
  70.